/*
    表单验证及提交
 */

var $phone = $("#loginPhone");
var $pwd = $("#loginPwd");
var $eyeOpen = $(".js-eye-open");
var $submitBtn = $("#loginBtn");

(function(){
  var phoneReg = /^[1][0-9]{10}$/; //手机号正则
  var pwdReg = /^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/; //密码正则
  var codeReg = /^[0-9]{4}$/; //验证码正则
  var wait = 60; //发送验证码等待时间

  //手机号输入框事件
  $phone.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var phone = $.trim($this.val());

      validateForm($this, phone, phoneReg, "请输入正确的手机号");
  });

  //密码输入框事件
  $pwd.on("onreadystatechange input blur", function(){
      var $this = $(this);
      var $cleartext = $this.next("input[type='text']");
      var pwd = $.trim($this.val());

      validateForm($this, pwd, pwdReg, "请输入正确的密码", $cleartext);
  });

  //查看明文密码
  $eyeOpen.on("click", lookPwd);

  //提交事件
  $submitBtn.on("click", loginSubmit);

  //表单验证, 前面四个都是必传参数：当前输入框、当前值、正则、提示文字，最后一个参数是可选参数：查看明文密码
  function validateForm(that, value, reg, txt, cleartext){
      var $parent = that.closest(".form-group");
      var $addon = $parent.find(".input-group-addon");
      var $msg = $parent.find(".js-line-alert");
      var $msgText = $parent.find(".js-alert-text");
      
      var flag = true;

      if(value.length === 0){
        modifyWarning($msgText, txt);
        showWarning($addon, that, $msg, cleartext);
        flag = false;
      }
      else if(!reg.test(value)){
        modifyWarning($msgText, txt);
        showWarning($addon, that, $msg, cleartext);
        flag = false;
      }
      else{
        hideWarning($addon, that, $msg, cleartext);
      }

      return flag;
  }

  //输入框边框变红并显示提示文字
  function showWarning(addon, input, msg, cleartext){
    if(cleartext){
      cleartext.addClass("input-error border-error");
    }
    addon.addClass("border-error");
    input.addClass("input-error border-error");
    msg.show();
  }

  //输入框边框恢复原色并隐藏提示文字
  function hideWarning(addon, input, msg, cleartext){
    if(cleartext){
      cleartext.removeClass("input-error border-error");
    }
    addon.removeClass("border-error");
    input.removeClass("input-error border-error");
    msg.hide();
  }

  //修改提示文字信息
  function modifyWarning(element, txt){
    element.text(txt);
  }

  //查看明文密码
  function lookPwd(){
    var $pwd = $(this).closest(".input-group").find("input[type='password']");
    var $pwd_show = $pwd.next("input[type='text']");
    var pwd = $pwd.val();
    var pwd_show = $pwd_show.val();

    if($pwd.is(":visible")){
        $pwd.hide();
        $pwd_show.val(pwd).show();
    }
    else{
        $pwd.val(pwd_show).show();
        $pwd_show.hide();
    }
  }

  //Ajax提交
  var Ajax = function(url, type, obj, success, error) {
      $.ajax({
          url: url,
          type: type,
          dataType: 'json',
          data: obj,
          timeout: 10000,
          success: function(json) {
            var data = json;

            success && success(data);
          },
          error: function(msg) {
            error && error(e);
          }
      });
  };

  //提交函数
  function loginSubmit(){
      $phone.blur();
      $pwd.blur();
    var phone = $.trim($phone.val());
    var pwd = $.trim($pwd.val());
    var flag = validateForm();

    /*if(flag){
      Ajax('login', 'post', {phone: phone, pwd: pwd}, function(data) {
        if (data.returnFlag == '0') {
          location.href = $('#returnPath').val() + $("#returnPage").val();
        }
        else {
          if (data.returnCode == 'A001') {
            alert("您的密码有误!")
          }
          else if (data.returnCode == 'A002') {
            alert("无此用户!")
          }
          else {
            alert("操作失败!");
          }
        }
      });
    }*/
  }
})();